import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
} from '@nestjs/common';
import { ArticleService } from './article.service';
import { Article } from './article.schema';
import { QueryDto, ArticleDto } from './article.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';

@ApiTags('文章')
@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) {}

  @Get()
  @ApiOperation({ summary: '获取所有文章' })
  async findAll(): Promise<Article[]> {
    return this.articleService.findAll();
  }

  @Get('condition')
  async findByCondition(@Query() query: QueryDto): Promise<Article[]> {
    return this.articleService.findByCondition(query);
  }

  @Get(':id')
  async findOne(@Param('id') id: string): Promise<Article> {
    return this.articleService.findOne(id);
  }

  @Post('tags')
  async calcTagArticleCount(
    @Body('tags') tags: string[],
  ): Promise<{ tag: string; count: number }[]> {
    return this.articleService.calcTagArticleCount(tags);
  }

  @Post()
  async create(@Body() article: ArticleDto): Promise<Article> {
    return this.articleService.create(article);
  }

  @Patch(':id')
  async update(
    @Param('id') id: string,
    @Body() article: Article,
  ): Promise<Article> {
    return this.articleService.update(id, article);
  }

  @Delete(':id')
  async delete(@Param('id') id: string): Promise<Article> {
    return this.articleService.delete(id);
  }
}
